package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.BoothPacketAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.PageBean;
import com.yiwei.zxp.api.bean.ShareGoldExplainBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class BoothPacketActivity extends BaseActivity {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private Unbinder unbiner;
    private BoothPacketAdapter adapter;
    private OnLoadeMoreListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booth_packet);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getComboList(1);
            }
        });
        listener = new OnLoadeMoreListener() {
            @Override
            public void onLoadMore(int page) {
                getComboList(page);
            }
        };
        list.addOnScrollListener(listener);
        adapter = new BoothPacketAdapter(this);
        list.setAdapter(adapter);
        getComboList(1);
    }

    public void getComboList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        PageBean bean = new PageBean();
        bean.current_page = page;
        bean.per_page = 10;
        Api.api_service.getComboList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                });
    }

    public void getExplain(String keyword) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ShareGoldExplainBean bean = new ShareGoldExplainBean();
        bean.keyword = keyword;
        Api.api_service.getShareGoldExplain(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showPacketTips(this, result.datas.title, result.datas.content);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(this, LoginNewActivity.class);
                    return;
                }
//                getExplain(Const.KeyWord.SHARED_GOLD);
                Util.startActivity(this, MyOrderActivity.class);
                break;
        }
    }
}
